<?php
class Catalog
{
	// Retrieve all genres
	public function GetAllGenres()
	{
		// Build SQL query
		$sql = 'CALL get_genres();';

		// Execute query
		return DatabaseHandler::GetAll($sql);
	}

	public function GetAllCategories()
	{
		// Build SQL query
		$sql = 'CALL get_categories();';

		// Execute query
		return DatabaseHandler::GetAll($sql);
	}

	public function GetMerchandiseByGenre($genreId, $pageNo, &$rHowManyPages)
	{
		// Build SQL query
		$sql = 'CALL count_merchandise_on_genre(:genreId)';

		// Build parameter array
		$params = array(':genreId' => $genreId);

		// Calculate the number of pages required to display the products
		$rHowManyPages = self::HowManyPages($sql, $params);       
		// Calculate the start item
		$startItem = ($pageNo - 1) * PRODUCTS_PER_PAGE;

		// Retrieve the list of products
		$sql = 'CALL get_merchandise_by_genre(:genreId, :products_per_page, :start_item)';
        
		// Build the parameters array
		$params = array(':genreId' => $genreId, ':products_per_page' => PRODUCTS_PER_PAGE, ':start_item' => $startItem);

		// Execute query
		return DatabaseHandler::GetAll($sql, $params);
	}

	public function GetMerchandiseByCategory($categoryId, $pageNo, &$rHowManyPages)
	{                                                             
		// Build SQL query
		$sql = 'CALL count_merchandise_on_category(:categoryId)';

		// Build parameter array
		$params = array(':categoryId' => $categoryId);  
		// Calculate the number of pages required to display the products
		$rHowManyPages = self::HowManyPages($sql, $params);     
		// Calculate the start item
		$startItem = ($pageNo - 1) * PRODUCTS_PER_PAGE;

		// Retrieve the list of products
		$sql = 'CALL get_merchandise_by_category(:categoryId, :products_per_page, :start_item)';

		// Build the parameters array
		$params = array(':categoryId' => $categoryId, ':products_per_page' => PRODUCTS_PER_PAGE, ':start_item' => $startItem);
                           
		// Execute query
		return DatabaseHandler::GetAll($sql, $params);
	}
    
    public function GetLatestReleases($maxItems)
    {                                                                                                                        
        // Build SQL query
        $sql = 'CALL get_latest_releases(:max_items)';

        // Build parameter array
        $params = array(':max_items' => $maxItems);                                                                           
                           
        // Execute query
        return DatabaseHandler::GetAll($sql, $params);
    }
    
    public function GetExpectedReleases($maxItems)
    {                                                                                                          
        // Build SQL query
        $sql = 'CALL get_expected_releases(:max_items)';

        // Build parameter array
        $params = array(':max_items' => $maxItems);                                                                           
                           
        // Execute query
        return DatabaseHandler::GetAll($sql, $params);
    }      
    
    public function GetSpecialDiscounts($maxItems)
    {                                                                                                                                           
        // Build SQL query
        $sql = 'CALL get_special_discounts(:max_items)';

        // Build parameter array
        $params = array(':max_items' => $maxItems);                                                                           
                           
        // Execute query
        return DatabaseHandler::GetAll($sql, $params);
    }
    
    public function GetMerchandiseDetails($merchandiseId)
    {                                                                                                                                                                                         
        // Build SQL query
        $sql = 'CALL get_merchandise_details(:merchandise_id)';
                                   
        // Build parameter array
        $params = array(':merchandise_id' => $merchandiseId);                                                                           
                           
        // Execute query                                     
        return DatabaseHandler::GetRow($sql, $params);
    }
    
    public function GetMerchandiseTracklist($merchandiseId)
    {                                                                                                                                                                                                                   
        // Build SQL query
        $sql = 'CALL get_merchandise_tracklist(:merchandise_id)';
                                   
        // Build parameter array
        $params = array(':merchandise_id' => $merchandiseId);                                                                           
                           
        // Execute query                                     
        return DatabaseHandler::GetAll($sql, $params);
    }
    
    public function GetProductLocations($merchandiseId)
    {
        //TO DO
    }

	/* Calculates how many pages of products could be filled by the
	 number of products returned by the $countSql query */              
	
    private static function HowManyPages($countSql, $countSqlParams)
    {
        // Create a hash for the sql query
        $queryHashCode = md5($countSql . var_export($countSqlParams, true));    
        // Verify if we have the query results in cache
        if (isset ($_SESSION['last_count_hash']) && isset ($_SESSION['how_many_pages']) && $_SESSION['last_count_hash'] === $queryHashCode)
        {
            // Retrieve the the cached value
            $how_many_pages = $_SESSION['how_many_pages'];
        }
        else
        {
            // Execute the query          
            $items_count = DatabaseHandler::GetOne($countSql, $countSqlParams);
            // Calculate the number of pages
            $how_many_pages = ceil($items_count / PRODUCTS_PER_PAGE);
            // Save the query and its count result in the session
            $_SESSION['last_count_hash'] = $queryHashCode;
            $_SESSION['how_many_pages'] = $how_many_pages;
        }
        // Return the number of pages
        return $how_many_pages;
    }
}
?>
